home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / osr5 / sco / scripts / mail / send < prev    next >
Encoding:
AWK Script  |  1997-08-26  |  5.5 KB  |  171 lines

  1. #!/usr/local/bin/gawk -f
  2. #!/u/johnd/bin/gawk -f
  3. # @(#) send.gawk 1.9 94/04/23
  4. # 91/02/26 john h. dubois iii (john@armory.com)
  5. # 91/04/25 added ability to handle multiple files, and progress report
  6. # 91/04/30 added help and ability to read stdin
  7. # 91/05/28 changed to use hostname instead of hard coded host name;
  8. #          removed hard coding of destination;
  9. #          fixed test for readability and X-Name of stdin
  10. # 91/07/22 changed to use env. variable SEND
  11. # 91/09/29 fixed to strip off entire directory component of filename
  12. # 91/11/08 added -u option; added splitting into MAXMAILMSG-size pieces
  13. # 92/05/03 Moved all code inside of awk script; changed to #!awk script
  14. # 92/05/11 Modified to not silently send an empty message (header only)
  15. #          when a file can not be read.  Now prints an error message & 
  16. #          does not send anything.
  17. # 92/11/22 Added -o option.
  18. # 94/03/09 Use gawk so - options can be given
  19. # 94/04/23 Use .sendrc
  20.  
  21. BEGIN {
  22.     rcfile = ".sendrc"
  23.     if (ARGV[1] ~ "^[-+]h") {
  24.     print \
  25. "send: send files by mail.\n" \
  26. "Syntax: send [-uho] [user] <file1> <file2> ...\n" \
  27. "If no file names are given, the standard input will be read.  The files\n" \
  28. "are sent by mail to the specified user.  If the first argument contains\n" \
  29. "a '@' or a '!', it is taken to be the name of the user to send the files\n" \
  30. "to.  Otherwise, the user is taken from the environment variable SEND, or\n"\
  31. "if it is not set, the file .sendrc in the invoking user's home directory.\n"\
  32. "The mail will have a subject of \"%%recfile%%\" and the header will\n" \
  33. "contain an extra field \"X-Name\" which gives the name of the file with\n" \
  34. "any directories removed from it, or with the name \"stdin\" if the\n" \
  35. "standard input was read for the file.  If the environment variable\n" \
  36. "MAXMAILMSG is set, the file will be split into multiple pieces, each with\n" \
  37. "a size less than or equal to MAXMAILMSG.  The first piece will have\n" \
  38. "the same X-Name that an unsplit file would.  The rest of the pieces\n" \
  39. "will have successive integers appended to the name, starting with 2.\n"\
  40. "Options:\n" \
  41. "-h: Print this help.\n" \
  42. "-u: Uuencode the file before sending.  The X-Name will be the original\n" \
  43. "    filename with \".u\" appended.  If the file is split, the sequence\n" \
  44. "    number will be appended after the \".u\".\n" \
  45. "-o: Send one file with a destination name different than the source name.\n" \
  46. "    Either one or two file names should be given.  The second or only name\n" \
  47. "    gives the name that the file should have at the destination.\n" \
  48. "    If one name is given, the file to send is read from stdin."
  49.     exit(0)
  50.     }
  51.  
  52.     MaxSize = ENVIRON["MAXMAILMSG"]
  53.     USER = ENVIRON["USER"]
  54.     NAME = ENVIRON["NAME"]
  55.     Date = strftime("%c")
  56. #    "date" | getline Date
  57.     "hostname" | getline HostName
  58.  
  59.     if (MaxSize == "")
  60.     MaxSize = 2000000000
  61.  
  62.     ArgInd = 1
  63.     while (ArgInd < ARGC && ARGV[ArgInd] ~ "^[-+]") {
  64.     if (ARGV[ArgInd] ~ "u")
  65.         UUEnc = 1
  66.     if (ARGV[ArgInd] ~ "o")
  67.         OneFile = 1
  68.     ArgInd++
  69.     }
  70.  
  71.     if (ARGV[ArgInd] ~ "[@!]")
  72.     Dest = ARGV[ArgInd++]
  73.     else if ("SEND" in ENVIRON)
  74.     Dest = SEND
  75.     else if ("HOME" in ENVIRON) {
  76.     rc = ENVIRON["HOME"] "/" rcfile
  77.     if ((getline line < rc) == 1)
  78.         Dest = line
  79.     close(rc)
  80.     }
  81.     if (Dest == "") {
  82.     print "No destination.  Exiting."
  83.     exit(1)
  84.     }
  85.  
  86.     ProtoHeader = \
  87.     "From: " USER "@" HostName " (" NAME ")\n" \
  88.     "To: " Dest "\n" \
  89.     "Subject: %%recfile%%\n" \
  90.     "Date: " Date "\n" \
  91.     "X-Name: "
  92.  
  93.     if (OneFile) {
  94.     DestFileName = ARGV[--ARGC]
  95.     if (ArgInd > ARGC) {
  96.         print "No destination filename given with +o.  Exiting."
  97.         exit 1
  98.     }
  99.     if ((ARGC - ArgInd) > 1) {
  100.         print "Too many filenames given with +o option.  Exiting."
  101.         exit 1
  102.     }
  103.     }
  104.  
  105.     if (ArgInd >= ARGC) {
  106.     if (SendFile("/dev/stdin",DestFileName,ProtoHeader))
  107.         printf "Error reading stdin\n"
  108.     }
  109.     else
  110.     for (; ArgInd < ARGC; ArgInd++)
  111.         if (SendFile(ARGV[ArgInd],DestFileName,ProtoHeader))
  112.         printf "Error reading %s\n",ARGV[ArgInd]
  113. }
  114.  
  115. function SendFile(File,RemoteName,ProtoHeader,  ret,DestFile) {
  116.     # RemoteName is the name the (possily uuencoded) file will be saved to at 
  117.     # the remote end.
  118.     # DestFile is the name the file will be given at the remote end
  119.     # (after uudecoding, if it is to be uuencoded).
  120.     # File is the name of the file being read.
  121.  
  122.     # Remove path
  123.     DestFile = File
  124.     sub(".*/","",DestFile)
  125.     if (RemoteName == "") {
  126.     RemoteName = DestFile
  127.     if (UUEnc)
  128.         RemoteName = RemoteName ".u"
  129.     }
  130.     if (UUEnc)
  131.     UUAct = " uuencoded"
  132.     printf "Sending \"%s\"%s as \"%s\"...\n",File,UUAct,RemoteName
  133.     Header = ProtoHeader RemoteName
  134.     FirstHeader = Header "\n\n"
  135.     CharsWritten = 0
  136.  
  137.     # BSD sh has no -x test
  138.     MailProc = \
  139.     "m=/usr/lib/mail/execmail;[ -f $m ]||m=/usr/lib/sendmail;exec $m " Dest
  140.  
  141.     SequenceNum = 1
  142.  
  143.     # Print header into mailproc after reading file to ensure that
  144.     # no mailproc is started up if file can not be read
  145.     if (UUEnc)
  146.     while ((ret = ("uuencode " File " " DestFile | getline Line)) == 1) {
  147.         ProcLine(FirstHeader Line)
  148.         FirstHeader = ""
  149.     }
  150.     else
  151.     while ((ret = (getline Line < File)) == 1) {
  152.         ProcLine(FirstHeader Line)
  153.         FirstHeader = ""
  154.     }
  155.     close(MailProc)
  156.     return ret
  157. }
  158.  
  159. function ProcLine(Line) {
  160.     CharsWritten += length(Line) + 1
  161.     # adding 0 to MaxSize neccessary for BSD awk
  162.     if (CharsWritten > (MaxSize + 0)) {
  163.     close(MailProc)
  164.     printf "Sending part %d...\d",++SequenceNum
  165.     printf Header SequenceNum "\n\n" "%s\n",Line | MailProc
  166.     CharsWritten = length(Line) + length(Header) + 3
  167.     }
  168.     else
  169.     printf "%s\n",Line | MailProc
  170. }
  171.